Thread: [C malloc/free] ~ free-ing the allocated memory gives c0000005 Windows error

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User Spiegel's Avatar
    Join Date
    Jul 2012
    Location
    Italy
    Posts
    14

    Question [C malloc/free] ~ free-ing the allocated memory gives c0000005 Windows error

    Hi everybody!

    I hope you all had a merry christmas... =)

    ....because I need your help!! >.<


    I'm new to C and I'm getting to like this language more and more at every step I take, but I keep banging my head against several walls.

    Today it's the "malloc/free" wall.

    I've built an exercise to learn how to pass values and memory pointers to functions. It's more like a simple "test my knowledge" thing, actually, but still good enough to understand and exercise logic.

    The exercise is very simple:
    - given two "int" variables, pass their values to a function that returns their sum; then print it on the screen
    - given a structure containing three "int" elements, allocate memory and pass its pointer to a function that calculates the product of the first two elements and saves it in the third one; then print it on the screen

    No problems AT ALL with the first part. Happy!!

    The second part, instead, works great until I use "free" to de-allocate memory.

    For a matter of speed, I've written it partly with italian words (my language), but I'll translate the "printf" messages so that you know what's happening.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    
    //----------------------------
    //----------------------------
    unsigned long secondary();
    
    int pass_struct ();
    
    struct test
    {
        int numero1;
        int numero2;
        int prodotto;
    };
    //----------------------------
    //----------------------------
    
    
    
    //----------------------------
    //----------------------------
    int main(void)
    {
        //---------------------------- PASSAGGIO DI DUE VALORI SEPARATI COME PARAMETRI PER UNA CHIAMATA AD UNA FUNZIONE
        unsigned long a, b, result;
    
    
        a = 2495383;
        b = 34934059;
    
        result = secondary (a, b);
    
        printf("a = %lu\nb = %lu\n\na+b = %lu\n\n", a, b, result);
    
        //----------------------------
    
    
        //---------------------------- PASSAGGIO DI UN PUNTATORE STRUTTURA COME PARAMETRO PER UNA CHIAMATA AD UNA FUNZIONE
        struct test * oggetto1;
    
        oggetto1 = malloc(sizeof(oggetto1));
    
        printf("Allocated oggetto1 at the address %p\n\n", oggetto1);
    
        oggetto1->numero1 = 2;
        oggetto1->numero2 = 7;
        oggetto1->prodotto = 0;
    
        oggetto1 = pass_struct(oggetto1); //<<-- Actually I shouldnt need to assign the returned value, it would be enough to write "pass_struct(oggetto1)"; right?
    
        printf("Executed call for pass_struct. Address oggetto1: %p\n\n\n", oggetto1);
    
        printf("numero1 = %i\nnumero2 = %i\nnumero1 * numero2 = %i\n\n", oggetto1->numero1, oggetto1->numero2, oggetto1->prodotto);
    
        free(oggetto1); //<<-- Why the heck does it give an error!?!?!??!? >.<
        //----------------------------
    
        return 0;
    }
    //----------------------------
    //----------------------------
    
    
    //----------------------------
    //----------------------------
    unsigned long secondary (unsigned  long uno, unsigned  long due)
    {
        return uno + due;
    }
    //----------------------------
    //----------------------------
    
    
    //----------------------------
    //----------------------------
    int pass_struct (struct test * pointer)
    {
        pointer->prodotto = pointer->numero1 * pointer->numero2;
    
        return pointer; //<<-- Absolutely useless, since the pointer doesnt change and it remains in the main function, aint it so? =o
    }
    //----------------------------
    //----------------------------

    The error comes out randomly, not at every execution; clear sign that it's bound to memory addresses, as if the address I'm trying to free is no longer reserved and gets used by another application.

    I've tried to analyze the whole thing excluding the most common errors like scopes, re-assignements and the likes.
    As you can see I've even set an extra "printf" after the call for the external function to ensure the address inside the "oggetto1" variable is still the same as before.

    I've already (made and) done another exercise that finds all the prime numbers between 3 and "n" (value given by user) and memorizes them in a "list" of structures; then frees all the allocated memory through the "free" function, but I get absolutely no errors, there. Argh!! >.<
    The "good side" is that it finds all the prime numbers between 3 and 40.000.000 in about 14 seconds. (Intel i7 920 @ 4.00GHz)
    The "bad side" is that it's written in a single block of code (awful!!), while I'd like to separate each routine in a different header file, but first I want to be sure I'm able to pass data safely between separate functions.

    I guess this error has something to do with passing the pointer to another function, but I cant understand how.

    Please, help!! =|


    PS: about the address I "return" from the "pass_struct" function and that I assign to "oggetto1", I've also re-written it to return absolutely nothing and not assign it to "oggetto1", but the error still happens.
    Just to be clear:
    - the function call would be simply "struct_pass(oggetto1);"
    - at the end of the "struct_pass" function there would be a simple "return 0;"

    PPS: I'm using "QT Creator" ver. 2.4.1, as compiler
    Last edited by Spiegel; 12-26-2012 at 10:41 AM. Reason: PPS:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to properly free a pointer and its allocated memory
    By yuzhangoscar in forum C Programming
    Replies: 2
    Last Post: 09-16-2008, 06:26 AM
  2. Can you free memory allocated by a std::string?
    By BrianK in forum C++ Programming
    Replies: 17
    Last Post: 05-15-2008, 11:57 AM
  3. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  4. Will free() clean up this allocated memory?
    By simguy in forum C Programming
    Replies: 3
    Last Post: 01-18-2007, 05:21 PM
  5. free allocated memory on interrupts
    By scrappy in forum C Programming
    Replies: 4
    Last Post: 02-20-2004, 11:13 AM

Tags for this Thread